home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / TUT20.ZIP / TUT20.TXT < prev    next >
Encoding:
Text File  |  1995-12-14  |  8.6 KB  |  210 lines

  1.                    ╒═══════════════════════════════╕
  2.                    │         W E L C O M E         │
  3.                    │  To the VGA Trainer Program   │ │
  4.                    │              By               │ │
  5.                    │      DENTHOR of ASPHYXIA      │ │ │
  6.                    ╘═══════════════════════════════╛ │ │
  7.                      ────────────────────────────────┘ │
  8.                        ────────────────────────────────┘
  9.  
  10.                            --==[ PART 20 ]==--
  11.  
  12.  
  13.  
  14. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  15. ■ Introduction
  16.  
  17. Hi all! It has been a _long_ time since my last trainer (as I am sure many
  18. of you have noticed) A lot has happened between now and the last trainer...
  19. but for once I won't bore you with the details ;) I do have a full time job
  20. though, coding C++ applications.
  21.  
  22. I have taken over the production of the PCGPE from Mark Feldman. He is
  23. mailing all the articles written so far, and as soon as I get them I will
  24. get to work on releasing the PCGPE II. Mark is working on the Windows GPE.
  25.  
  26. This trainer is on 3d hidden face removal and face sorting. I was going to
  27. add shading, but that can wait until a later trainer. For conveniance I
  28. will build on the 3d code from tut 16(?). The maths for face removal is a
  29. bit tricky, but just think back to your old High School trig classes.
  30.  
  31. I have noticed that in my absence, one or two people have started their own
  32. trainer series. Read Hornet DemoNews for a great column by Trixter covering
  33. some of the more tricky demo effects.
  34.  
  35. Well, on with the trainer!
  36.  
  37.  
  38. If you would like to contact me, or the team, there are many ways you
  39. can do it : 1) Write a message to Grant Smith/Denthor/Asphyxia in private mail
  40.                   on the ASPHYXIA BBS.
  41.             2) Write to :  Grant Smith
  42.                            P.O.Box 270 Kloof
  43.                            3640
  44.                            Natal
  45.                            South Africa
  46.             3) Call me (Grant Smith) at (031) 73 2129 (leave a message if you
  47.                   call during varsity). Call +27-31-73-2129 if you call
  48.                   from outside South Africa. (It's YOUR phone bill ;-))
  49.             4) Write to denthor@beastie.cs.und.ac.za in E-Mail.
  50.             5) Write to asphyxia@beastie.cs.und.ac.za to get to all of
  51.                us at once.
  52.  
  53. NB : If you are a representative of a company or BBS, and want ASPHYXIA
  54.        to do you a demo, leave mail to me; we can discuss it.
  55. NNB : If you have done/attempted a demo, SEND IT TO ME! We are feeling
  56.         quite lonely and want to meet/help out/exchange code with other demo
  57.         groups. What do you have to lose? Leave a message here and we can work
  58.         out how to transfer it. We really want to hear from you!
  59.  
  60. http://goth.vironix.co.za/~denthor                     (WWW)
  61. ftp.eng.ufl.edu pub/msdos/demos/code/graph/tutor       (FTP)
  62.  
  63.  
  64. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  65. ■  Face Sorting
  66.  
  67. There are many ways to sort faces in a 3d object. For now, I will show you
  68. just about the easiest one of the lot.
  69.  
  70. Say you have to polygons....
  71.  
  72.                 ------P1
  73.  
  74.            ------------------P2
  75.  
  76.                    Eye
  77.  
  78. As you can see, P1 has to be drawn before P2. The easiest way to do this is
  79. as follows:
  80.  
  81. On startup, find the mid point of each of the polys, through the easy
  82. equations,
  83.         x = (P2.1.x + P2.2.x + P2.3.x + p2.4.x)/4
  84.         y = (P2.1.y + P2.2.y + P2.3.y + p2.4.y)/4
  85.         z = (P2.1.z + P2.2.z + P2.3.z + p2.4.z)/4
  86.  
  87. NOTE : For a triangle you would obviously only use three points and divide
  88. by three.
  89.  
  90. Anyway, now you have the X,Y,Z of the midpoint of the polygon. You can then
  91. rotate this point with the others. When it comes time to draw, you can
  92. compare the resulting Z of the midpoint, sort all of the Z items, and then
  93. draw them from back to front.
  94.  
  95. In the sample program I use a simple bubble sort... basically, I check the
  96. first two values against each other, and swap them if the first is bigger
  97. then the second. I continue doing this to all the numbers until I run
  98. through the entire list without swapping once. Bubble sorts are standard
  99. seven computer science topics... perhaps borrow a text book to find out
  100. more about them and other (better) sorting methods.
  101.  
  102. The above isn't perfect, but it should work 90% of the time. But it still
  103. means that when you are drawing a cube, you have to draw all 6 sides every
  104. frame, even though only three or so are visible. That is where hidden face
  105. removal comes in...
  106.  
  107. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  108. ■  Hidden Face Removal
  109.  
  110. Pick up something square. A stiffy disk will do fine. Face it towards you,
  111. and number all the corners from one to four in a clockwise direction.
  112.  
  113.                 1 +-------------+ 2
  114.                   |             |
  115.                   |             |
  116.                   |             |
  117.                   |             |
  118.                 4 +-------------+ 3
  119.  
  120. Now rotate the stiffy disk on all three axese, making sure that you can
  121. still see the front of the disk. You will notice that whenever you can see
  122. the front of the disk, the four points are still in alphabetical order. Now
  123. rotate it so that you can see the back of the stiffy. Your points will now
  124. be :
  125.  
  126.                 2 +-------------+ 1
  127.                   |             |
  128.                   |             |
  129.                   |             |
  130.                   |             |
  131.                 3 +-------------+ 4
  132.  
  133. The points are now anti-clockwise! This means, in it's simplest form, that
  134. if you define all your poygon points in a clockwise order, when drawing you
  135. ignore the polys that are anticlockwise. (Obviously when you define the 3d
  136. object, you define the polygons facing away from you in an anticlockwise
  137. order)
  138.  
  139. To find out weather a poly's points are clockwise or not, we need to find
  140. it's normal. Here is where things start getting fun.
  141.  
  142. In school, you are told that a normal is perpendicular to the plane. In
  143. ascii :
  144.                       | Normal
  145.                       |
  146.                       |
  147.         --------------------------- Polygon
  148.  
  149. As you can see, the normal is at 90 degrees to the surface of the poly. We
  150. must extend this to three dimensions for our polygons. You'll have to trust
  151. me on that, I can't draw it in ascii :)
  152.  
  153. To find a normal, you only need three points from your poly (ABC) :
  154. A(x0,y0,z0), B(X1,Y1,Z1), C(X2,Y2,Z2)
  155.  
  156. then the vector normal = AB^AC = (Xn,Yn,Zn) with
  157.         Xn=(y1-y0)(z0-z2)-(z1-z0)(y0-y2)
  158.         Yn=(z1-z0)(x0-x2)-(x1-x0)(z0-z2)
  159.         Zn=(x1-x0)(y0-y2)-(y1-y0)(x0-x2)
  160.  
  161. We are interested in the Z normal, so we will use the function :
  162.   normal:=(x1-x0)(y0-y2)-(y1-y0)(x0-x2);
  163.  
  164. The result is something of a sine wave when you rotate the poly in three
  165. dimensions. A negative value means that the poly is facing you, a posotive
  166. value means that it is pointing away.
  167.  
  168. The above means that with a mere two muls you can discount an entire poly
  169. and not draw it. This method is perfect for "closed" objects such as cubes
  170. etc.
  171.  
  172. I am anything but a maths teacher, so go borrow someones math book to find
  173. out more about surface normals. Trust me, there is a lot more written about
  174. them then you think.
  175.  
  176. An extension of calculating your normal is finding out about light-sourcing
  177. your polygons. Watch for more information in one of the next few tutors.
  178.  
  179.  
  180. A combination of the above two routines should work quite nicely in
  181. creating 3d objects with little or no overlapping. The example file will
  182. show you the two methods and how well they work.
  183.  
  184.  
  185. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  186. ■  In closing
  187.  
  188. As you can see, the above was quite easy. I have a few ideas for tut 21, so
  189. keep watch for it. Also keep an eye open for PCGPE ][ (but don't mail me
  190. asking when it's due! I already get too many of those! ;-)
  191.  
  192. My sister got married a few days ago. The worst part was that I was forced
  193. to cut my hair. My hair was quite long (slightly longer then when the pic
  194. on my web page was taken), and it is all quite depressing. Anyway, the
  195. wedding was great, so it wasn't all for nothing.
  196.  
  197. I hope to get tut 21 and possibly 22 out before christmas, but I will be on
  198. holiday from the 18th. I will be in Cape Town sometime after christmas day
  199. for a week or two, so if you're there I'll meet you on the cable car :-)
  200.  
  201. I wrote a quote for this tut, but I have decided I didn't like it. I'll try
  202. do better for tut 21 ;)
  203.  
  204. Byeeeee.....
  205.   - Denthor
  206.       14-12-95
  207.  
  208. PS. I seem to have lost my list of distribution sites... could you all
  209. re-mail me your details? Thanks.
  210.